<template>
  <div class="bottom-bar">
    <div class="bottom-img">
      <check-button class="check-button" :isChecked="isCheckAll" @click.native="checkAll"></check-button>
      <span>全选</span>
    </div>

    <div class="total-price">
      合计：{{totalPrice}}元
    </div>

    <div class="calculate">
      去计算    ({{checkLength}})
    </div>
  </div>
</template>

<script>
  import CheckButton from "components/contents/checkButton/CheckButton";


  export default {
    name: "CartBottomBar",
    components:{
      CheckButton,
    },
    computed:{
      totalPrice(){
        return '￥'+ this.$store.state.cartList.filter(item=>{
          return item.checked;
        }).reduce((preValue,item)=>{
          return item.price*item.count + preValue
        },0).toFixed(2)
      },
      checkLength(){
        return this.$store.state.cartList.filter(item=>{
          return item.checked;
        }).length
      },
      isCheckAll(){
        // return !(this.$store.state.cartList.filter(item=>{return !item.checked}).length)
        if(this.$store.state.cartList.length===0) return false;
        return !(this.$store.state.cartList.find(item=>!item.checked));
      },
    },
    methods:{
      checkAll(){
        console.log('选择所有');
        if(this.isCheckAll){
          this.$store.state.cartList.forEach(item=>item.checked=false)
        }else{
          this.$store.state.cartList.forEach(item=>item.checked=true)
        }
      }
    }
  }
</script>

<style scoped>
  .bottom-bar {
    height: 40px;
    position: relative;
    background-color: #eee;
    display: flex;

    text-align: center;
    font-size: 16px;
  }
  .bottom-img {
    padding-top: 5px;
    display: flex;
  }
  .bottom-img .check-button {
    margin-right: 10px;
  }
  .total-price {
    padding-top: 5px;
    margin-left: 25px;
  }
  .calculate {
    position: absolute;

    right: 0;
    bottom: 0;
    text-align: center;
    line-height: 40px;
    width: 120px;
    height: 40px;
    background-color: var(--color-high-text);
    color: var(--color-background);

  }
</style>
